package com.ai.zuochengyun.phase01.class03;

import java.util.LinkedList;
import java.util.Queue;

public class Code24_MaxSubCompSize {

    public static boolean isCompleteTree1(BinaryTreeNode x) {
        if (x == null) {
            return true;
        }

        Queue<BinaryTreeNode> queue = new LinkedList<>();
        queue.add(x);
        // 是否遇到两个孩子不双全的情况
        boolean leaf = false;
        while (!queue.isEmpty()) {
            BinaryTreeNode cur = queue.poll();
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }

            // 遇到两个孩子不双全的情况，但是又出现了不是叶子结点的情况
            if (leaf && (cur.left != null || cur.right != null)) {
                return false;
            }
            // 如果没有左孩子，但有右孩子
            if (cur.left == null && cur.right != null) {
                return false;
            }

            // 不是两个子节点都有的场景
            if (cur.left == null || cur.right == null) {
                leaf = true;
            }
        }
        return true;
    }

    public static boolean isCompleteTree2(BinaryTreeNode x) {
        return process(x).isCBT;
    }

    public static Info process(BinaryTreeNode x) {
        if (x == null) {
            return new Info(0, true, true);
        }

        // 从左边要信息
        Info leftInfo = process(x.left);
        // 从右边要信息
        Info rightInfo = process(x.right);

        // 以X为头节点的树的高度
        int height = Math.max(leftInfo.height, rightInfo.height) + 1;

        // 左树是全二叉树，右树是全二叉树  并且两边的树的高度是一样的
        boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;

        boolean isCBT = false;

        if (leftInfo.height == rightInfo.height) {
            // 左树和右树高度一样时有两种情况
            // 一是左边满 右边也满  二是左边是满的 右边是完全二叉树
            isCBT = (leftInfo.isFull && rightInfo.isFull) || (leftInfo.isFull && rightInfo.isCBT);
        } else if (leftInfo.height == rightInfo.height + 1) {
            // 左树比右树高度大1时有两种情况
            // 一是左边是完全二叉树 右边是满二叉树  二是左边是满的 右边的完全二叉树
            isCBT = (leftInfo.isCBT && rightInfo.isFull) || (leftInfo.isFull && rightInfo.isCBT);
        }
        return new Info(height, isFull, isCBT);
    }

    private static BinaryTreeNode generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }


    private static BinaryTreeNode generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        BinaryTreeNode head = new BinaryTreeNode((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 5;
        int maxValue = 100;
        int testTimes = 100000;
        for (int i = 0; i < testTimes; i++) {
            BinaryTreeNode head = generateRandomBST(maxLevel, maxValue);
            if (isCompleteTree1(head) != isCompleteTree2(head)) {
                System.out.println("oops");
                break;
            }
        }
        System.out.println("finish!");
    }


    public static class Info {
        // 树的高度
        public int height;

        // 是否是满二叉树
        public boolean isFull;

        // 是否是完全二叉树
        public boolean isCBT;

        public Info(int height, boolean isFull, boolean isCBT) {
            this.height = height;
            this.isFull = isFull;
            this.isCBT = isCBT;
        }
    }
}
